$(function () {
    syb('#signOrgId');
    var checkResult = checkHasSybAuth($("#signOrgId option"));
    $('#staleTable').dataTableExtend({
        "ajax": sysInfo.basePath + "/zs/credential/qualification/get_stale_data.do",
//			    	"select":true,
        "scrollX": "true",//是否开启横项滚动
        "fixedColumns": {//锁定列，只有开启scrollX时才生效
            "leftColumns": 4,//左侧锁定2列
            // "rightColumns": 1//右侧锁定1列
        },
        "fnServerParams": function (aoData) {
            //搜索条件
            var checkResult = checkHasSybAuth($('#signOrgId option'));
            if (!checkResult) {
                aoData.isAll = "0";
            }
            aoData.signOrgId = $('[name="signOrgId"]').val();
            aoData.certificateType = $("#certificateType").val();
            aoData.certificateName = $("#certificateName").val();
            aoData.certificateLevel = $("#certificateLevel").val();
            aoData.overdueStatus = $("#overdueStatus").val();
            aoData.applicationScope = $("#applicationScope").val();
            aoData.conditions = $('[name="conditions"]').val();
        },
        "aoColumns": [
            {
                "title": "序号",
                "orderable": false,
                "render": function (data, type, full, meta) {
//			                    console.log(meta.row);
                    return meta.row + 1;
                }
            },
            {
                "title": "所属事业部",
                "data": "signOrgName",
                "orderable": false
            },
            {
                "title": "资质编号",
                "data": "certificateNo",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return "<a href='javascript:void(0);'  class='layui-table-link' onclick='qualificationDetails(\"" + data + "\",\"" + full.id + "\")'>" + data + "</a>";
                }
            },
            {
                "title": "资质所属企业",
                "data": "companyName",
                "orderable": false
            },
            {
                "title": "资质有效期",
                "data": "certificateExpiryTime",
                "orderable": false,
                "render": function (data) {
                    return data.substring(0, 10);
                }
            },
            {
                "title": "资质类型",
                "data": "certificateType",
                "orderable": false
            },
            {
                "title": "资质名称",
                "data": "certificateName",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    data = (null != data && "" != data) ? data : "";
                    return "<div title='" + data + "' class='f-toe' style=\"max-width:100px;word-wrap:break-word\">" + data + "</div>";
                }
            },
            {
                "title": "资质等级",
                "data": "certificateLevel",
                "orderable": false
            },
            {
                "title": "资质预警提醒",
                "data": "certificateExpiryTime",
                "orderable": false,
                "render": function (data) {
                    return addLabel(data, "资质已过期", "资质过期剩余")
                }
            },
            {
                "title": "操作",
                "data": "id",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return '<a onclick="edit(\'' + data + '\',\'' + full.certificateStatus + '\')" class="iboss-link"  style="cursor:pointer">编辑</a>';
                }
            }
        ],
    });
});

function earlyWarning() {
    dgg.Open({
        title: '库容预警量设置',
        content: $('#earlyWarningTime'),
        area: '400px',
        yes: function (index) {
            var staleValue = $("[name='staleValue']").val();
            $.ajax({
                type: "post",
                url: sysInfo.basePath + "/zs/warning_configure/update_single_warning.do",
                data: {
                    warningType: "ZZ_CREDENTIAL",
                    warningName: "CERTIFICATE_EXPIRE_WARNING",
                    warningValue: staleValue
                },
                dataType: "json",
                success: function (result) {
                    if (result.code == 0) {
                        $("[name='staleValue']").val(staleValue);
                        $("#textExpireWarning").text(staleValue);
                        layer.close(index);
                        DGG.Succeed('保存成功!');
                        searchTable();
                    } else {
                        DGG.Errors(result.msg);
                    }
                }
            });
        }
    })
}

//刷新
function searchTable() {
    $('#staleTable').dataTableExtend("reload");
}

function staleValueDo(nStr) {
    if (nStr != null && nStr != "") {
        nStr = parseInt(nStr);
    }
    $("[name='staleValue']").val(nStr);
}


//根据资质类型茶资质名称
$("#certificateType").on("change", function () {
    findCertificateName("190px","#certificateType", "#certificateName", "ZZ_CREDENTIAL");
});
//根据资质名称茶资质等级
// $("#certificateName").on("change", function () {
//     findCertificateLevel("#certificateName", "#certificateLevel");
// });

function edit(id, status) {
    var adminEdit = $("[name='adminEdit']").val();
    if (status == "CERTIFICATE_APTITUDE_STATUS_1" || !checkIsEmpty(adminEdit)) {
        DGG.loading();
        $.post(sysInfo.basePath + "/zs/credential/qualification/edit_page.html", {id: id}, function (data) {
            DGG.loading('close');
            dgg.Open({
                type: 1,
                title: '编辑资质信息',
                content: data,
                area: '80%',
                yes:function (index) {
                    var params = $('#qualificationForm').serializeObject();
                    if(!$("#qualificationForm [name='certificateType']").val()){
                        DGG.Warning("资质类型不能为空");
                        return false;
                    }
                    if(!$("#qualificationForm #certificateName").val()){
                        DGG.Warning("资质名称不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='certificateLevel']").val()){
                        DGG.Warning("资质等级不能为空");
                        return;
                    }
                    if(!$("#qualificationForm [name='applicationScope']").val()){
                        DGG.Warning("适用范围不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='companyName']").val()) {
                        DGG.Warning("资质所属企业不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='certificateNo']").val()) {
                        DGG.Warning("资质编号不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='receiptPrice']").val()){
                        DGG.Warning("成本价格不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='certificateExpiryTime']").val()){
                        DGG.Warning("资质过期时间不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='certificateArea']").val()){
                        DGG.Warning("地区不能为空");
                        return;
                    }
                    var certificateName = "";
                    $.each($("#qualificationForm #certificateName").select2("val"),function (i,v) {
                        certificateName += v +",";
                    });
                    if(checkEmpty(certificateName)){
                        certificateName = certificateName.substring(0,certificateName.length -1);
                        params.certificateName =certificateName;
                    }
                    if (certificateName.split(",").length>5) {
                        DGG.Warning("最多只能选择五个资质名称！");
                        return;
                    }
                    var index1 = DGG.Confire("是否保存数据？","保存提示",function() {
                        DGG.loading();
                        $.post(sysInfo.basePath+"/zs/credential/qualification/save_edit.do", params,function (re) {
                            DGG.loading('close');
                            if (re.code == 0) {
                                layer.close(index);
                                reloadTable();
                                DGG.Succeed("保存成功！");
                            } else {
                                DGG.Errors(re.msg);
                            }
                        },'json');
                    });
                }
            });
        }, 'html');
    } else {
        layer.msg("只能修改状态为未使用的资质！")
    }
}